﻿using MongoDB.Bson;
using CRL.Data.LambdaQuery;
using CRL.Data.LambdaQuery.CRLExpression;
using System;

namespace CRL.Mongo
{
    internal class FilterData
    {
        public BsonDocument Filter;
        public object Data;
        public CRLExpressionType Type;
        public Type ModelType;
    }
    class binaryObj
    {
        public string op;
        public FilterData left;
        public FilterData right;
        public bool pipeline;
        BsonValue getName(LambdaQueryBase query, FilterData d)
        {
            if (d.Type == CRLExpressionType.Value)
            {
                return BsonValue.Create(d.Data);
            }
            var prefix = query.GetPrefix(d.ModelType, "").Replace(".", "_");
            if (prefix == "t1_")
            {
                prefix = "$";
            }
            else
            {
                prefix = $"$${prefix}";
            }
            return $"{prefix}{d.Data}";
        }
        public BsonDocument ToBson(LambdaQueryBase query)
        {
            if (pipeline)
            {
                return new BsonDocument($"${op}", new BsonArray().Add(getName(query, left)).Add(getName(query, right)));
            }
            return new BsonDocument(left.Data.ToString(), new BsonDocument($"${op}", BsonValue.Create(right.Data)));
        }
    }

}
